﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class VideoClipList : System.Web.UI.Page
{
    static int pageSize = 7;
    int pid;

    protected void Page_Load(object sender, EventArgs e)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        string pid_s = Request.QueryString["pid"];
        bool t = Int32.TryParse(pid_s, out pid);

        if ((t && pid <= 0) || (!t))
        {
            pid = 1;
        }

        LoadVideo(pid);
    }

    private void LoadVideo(int pid)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        titleDetails.InnerText = "Video";

        var videolst = (from i in db.VideoClips
                        select new
                        {
                            VideoID = i.VideoID,
                            Title = i.Title,
                            AlbumID = i.AlbumID,
                            AlbumName = i.Album.AlbumName,
                            ArtistID = i.Album.ArtistID,
                            ArtistName = i.Album.Artist.ArtistName,
                            Rating = i.Rating,
                            Poster = i.Poster,
                            Date = i.Album.Date
                        }).OrderByDescending(i => i.Date).Skip((pid - 1) * pageSize).Take(pageSize);

        Rpt_MusicList.DataSource = videolst;
        Rpt_MusicList.DataBind();

        int numItems = db.VideoClips.Count();
        int numPages = numItems / pageSize;
        if (numItems % pageSize != 0)
        {
            numPages++;
        }
        BuildPaggingControls(numPages);
    }

    private void BuildPaggingControls(int numPages)
    {
        paggingControls.Controls.Clear();
        if (numPages > 1)
        {
            for (int i = 1; i <= numPages; i++)
            {
                LinkButton lnk = new LinkButton();
                lnk.Text = " " + i + " ";
                lnk.PostBackUrl = "VideoClipList.aspx?pid=" + i;
                paggingControls.Controls.Add(lnk);
            }
        }
    }
}